/** 
 * SymbolTableFrame.java
 * @author Lex
 * 2007-8-23 ����06:54:08
 */
package fun.ui.frames;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JToolBar;

import fun.symtable.SymbolTable;
import fun.symtable.UserSymbolTable;
import fun.symtable.tablemodel.FunctionTableModel;
import fun.symtable.tablemodel.SymbolTableModel;
import fun.symtable.tablemodel.VariableTableModel;
import fun.ui.FunMainFrame;

public class SymbolTableFrame extends BasicInternalFrame {
    private JPanel pane = null;
    private JToolBar mainToolBar = null;
    private JScrollPane scrollPane = null;
    private JButton refreshButton = null;
    private JButton drawButton = null;
    private JTable table = null;
    private JComboBox comboBox = null;
    // ================================
    private SymbolTable st = UserSymbolTable.getInstance();
    private String[] displayNames = { "Variables", "Functions" };
    private SymbolTableModel[] models = { new VariableTableModel(), new FunctionTableModel() };

    public SymbolTableFrame(FunMainFrame mainframe) {
	super(mainframe);
	initialize();
	this.refreshTable();
    }

    @SuppressWarnings("unchecked")
    private void refreshTable() {
	int index = this.getComboBox().getSelectedIndex();
	if (-1 == index)
	    return;
	SymbolTableModel model = models[index];
	if (1 == index)
	    model.setData(st.functions());
	else
	    model.setData(st.variables());
    }

    // =======================
    private void initialize() {
	this.setTitle("Symbol Table");
	this.setContentPane(getPane());
    }

    private JPanel getPane() {
	if (pane == null) {
	    pane = new JPanel();
	    pane.setLayout(new BorderLayout());
	    pane.add(getMainToolBar(), BorderLayout.NORTH);
	    pane.add(getScrollPane(), BorderLayout.CENTER);
	}
	return pane;
    }

    private JScrollPane getScrollPane() {
	if (scrollPane == null) {
	    scrollPane = new JScrollPane();
	    scrollPane.setViewportView(getTable());
	}
	return scrollPane;
    }

    private JTable getTable() {
	if (table == null) {
	    table = new JTable();
	}
	return table;
    }

    private JToolBar getMainToolBar() {
	if (mainToolBar == null) {
	    mainToolBar = new JToolBar();
	    mainToolBar.add(getRefreshButton());
	    mainToolBar.add(getComboBox());
	    mainToolBar.addSeparator();
	    mainToolBar.add(this.getDrawButton());
	    mainToolBar.add(Box.createHorizontalGlue());
	}
	return mainToolBar;
    }

    private JButton getRefreshButton() {
	if (refreshButton == null) {
	    refreshButton = new JButton();
	    refreshButton.setText("Refresh");
	    refreshButton.addActionListener(new ActionListener() {
		public void actionPerformed(ActionEvent e) {
		    refreshTable();
		}
	    });
	}
	return refreshButton;
    }

    private JButton getDrawButton() {
	if (null == this.drawButton) {
	    this.drawButton = new JButton("Draw");
	    this.drawButton.addActionListener(new ActionListener() {

		FunctionTableModel model = (FunctionTableModel) models[1];

		public void actionPerformed(ActionEvent e) {
		    int[] rows = getTable().getSelectedRows();
		    if (0 == rows.length)
			return;
		    FunctionFrame frame = new FunctionFrame(mainframe);
		    for (int i = 0; i < rows.length; i++)
			frame.addFunction(model.getValue(rows[i]));

		    mainframe.addInternalFrame(frame);
		}
	    });
	}
	return this.drawButton;
    }

    private JComboBox getComboBox() {
	if (comboBox == null) {
	    comboBox = new JComboBox(displayNames);
	    comboBox.addActionListener(new ActionListener() {

		public void actionPerformed(ActionEvent e) {
		    int index = comboBox.getSelectedIndex();
		    if (-1 == index)
			return;
		    if (1 == index)
			getDrawButton().setEnabled(true);
		    else
			getDrawButton().setEnabled(false);
		    getTable().setModel(models[index]);
		    refreshTable();
		}
	    });
	}
	return comboBox;
    }
}
